package dk.henrikkleist.knrradio;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class KNRRadio extends Activity implements OnClickListener {

	private Button buttonPlay;

	private Button buttonStopPlay;

	private WebView webView;

	private String LOG_TAG = "AndroidWebViewActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		buttonPlay = (Button) findViewById(R.id.buttonPlay);
		buttonPlay.setOnClickListener(this);

		buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
		buttonStopPlay.setOnClickListener(this);
		buttonStopPlay.setEnabled(false);

		webView = (WebView) findViewById(R.id.webView);

		// loads the WebView completely zoomed out
		webView.getSettings().setLoadWithOverviewMode(false);

		// true makes the Webview have a normal viewport such as a normal
		// desktop browser
		// when false the webview will have a viewport constrained to it's own
		// dimensions
		webView.getSettings().setUseWideViewPort(false);

		// override the web client to open all links in the same webview
		webView.setWebViewClient(new MyWebViewClient());
		webView.setWebChromeClient(new MyWebChromeClient());

		// Injects the supplied Java object into this WebView. The object is
		// injected into the
		// JavaScript context of the main frame, using the supplied name. This
		// allows the
		// Java object's public methods to be accessed from JavaScript.
		webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

		// load the home page URL
		String data = "" +
				"<html>" +
				"	<body bgcolor=\"#858585\" background=\"http://www.liamkleist.eu/knr/knr.radio.bg.png\">" +
				"		<div id=\"container\" style=\"width:100%\">" +
				"			<div id=\"header\" style=\"height=15%\">" +
				"			<iframe frameborder=\"0\" height=\"15%\" name=\"\" src=\"http://tulugaq.gl/shared/knr/nu.aspx?type=radio&lang=dk\" allowTransparency=\"true\" width=\"100%\"></iframe>" +
				"			</div>" +
				"			<div id=\"content\" style=\"height:85%;width:100%;float:left;\">" +
				"			<font color=\"#FFFFFF\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">" +
				"			<strong>Dagens program</strong>:" +
				"			<br>" +
				"			<iframe frameborder=\"0\" height=\"85%\" name=\"\" scrolling=\"auto\" src=\"http://tulugaq.gl/shared/knr/dagensprogram.aspx?type=radio&lang=dk\" allowTransparency=\"true\" width=\"100%\"></iframe>" +
				"			</div>" +
				"		</div>" +
				"	</body>" +
				"</html>";
		webView.loadData(data, "text/html", null);
	}

	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (Uri.parse(url).getHost().equals("demo.mysamplecode.com")) {
				return false;
			}
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
			startActivity(intent);
			return true;
		}
	}

	private class MyWebChromeClient extends WebChromeClient {

		// display alert message in Web View
		@Override
		public boolean onJsAlert(WebView view, String url, String message,
				JsResult result) {
			Log.d(LOG_TAG, message);
			new AlertDialog.Builder(view.getContext()).setMessage(message)
					.setCancelable(true).show();
			result.confirm();
			return true;
		}

	}

	public class JavaScriptInterface {
		Context mContext;

		// Instantiate the interface and set the context
		JavaScriptInterface(Context c) {
			mContext = c;
		}

		// using Javascript to call the finish activity
		public void closeMyActivity() {
			finish();
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonPlay:
			playService();
			break;

		case R.id.buttonStopPlay:
			stopPlayService();
			break;
		}
	}

	private void stopPlayService() {
		stopService(new Intent(this, RadioService.class));
		buttonPlay.setEnabled(true);
		buttonStopPlay.setEnabled(false);
	}

	private void playService() {
		startService(new Intent(this, RadioService.class));
		buttonPlay.setEnabled(false);
		buttonStopPlay.setEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.knrradio, menu);
		return true;
	}
}
